const app = getApp()
import axios from '../../utils/axios'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shoppList: [],
    cartsId: '',
    realityMoney: 0,
    sumMoney: 0,
    sendCost: 0,
    fullFree: 0,
    denomination: '',
    orderId: '',
    createTime: '',
    orderNum: '',
    message: '',
    address: {
      userName: '',
      userPhone: '',
      province: '',
      details: '',
      contactId: '',
      postcode: ''
    },
    jsApiParameters: ''
  },

  onLoad: function (options) {
    this.setData({
      count: options.count,
      orderId: options.orderId,
      createTime: options.createTime,
      orderNum: options.orderNum,
      denomination: parseFloat(options.denomination),
      shoppList: wx.getStorageSync('shoppList')
    })

    this.calculateList()

    this.carriage()

    this.getAddressInfo()

  },

  // 填充地址信息
  getAddressInfo: function () {
    const self = this
    wx.getStorage({
      key: 'paymentAddress',
      success(res) {
        console.log(res)
        self.setData({
          'address.userName': res.data.userName,
          'address.userPhone': res.data.userPhone,
          'address.province': res.data.province,
          'address.details': res.data.details,
          'address.contactId': res.data.contactId,
          'address.postcode': res.data.postcode
        })
      }
    })
  },

  onShow: function () {

  },

  cancelGoods: function () {
    let self = this
    let orderId = this.data.orderId

    wx.showModal({
      content: '确定取消该订单吗？',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          axios('/cloud/cancelGoodsOrder', {
            order_id: orderId
          }).then(res => {

            if (res.data.resultcode == 1) {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1300
              })
              setTimeout(function () {
                wx.redirectTo({
                  url: '/pages/orderInfo/orderInfo'
                })
              }, 1000)
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1300
              })
            }

          })
        }
      }
    })
  },

  // 提交订单
  orderGoods: function (e) {

    axios('/cloud/goodsOrderPay', {
      order_id: this.data.orderId,
      type: 'cloud'
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {

        if (res.data.result.length == 0) {
          wx.redirectTo({
            url: '/pages/orderInfo/orderInfo'
          })
        } else {

          this.setData({
            jsApiParameters: res.data.result
          })

          let payment = JSON.parse(res.data.result.jsApiParameters)

          wx.requestPayment({
            nonceStr: payment.nonceStr,
            package: payment.package,
            paySign: payment.paySign,
            timeStamp: payment.timeStamp,
            signType: payment.signType,
            success(pay) {
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 1000
              })
              setTimeout(function () {
                wx.redirectTo({
                  url: '/pages/orderInfo/orderInfo'
                })
              }, 1000)

            },
            fail: err => {
              wx.hideLoading()
              wx.showToast({
                title: '取消支付',
                icon: 'none'
              })
            }
          })

        }

      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1300
        })
      }
    })

  },

  // 处理商品list
  calculateList: function () {

    let list = this.data.shoppList

    let sumMoney = 0

    let cartsId = ''

    for (let i = 0; i < list.length; i++) {

      cartsId += list[i].cart_id

      if (i != list.length - 1) {
        cartsId += ','
      }

      sumMoney += parseFloat(list[i].showCount)

    }

    this.setData({
      cartsId: cartsId,
      realityMoney: parseFloat(parseFloat(sumMoney) - this.data.denomination).toFixed(2),
      sumMoney: parseFloat(sumMoney).toFixed(2)
    })

  },

  carriage: function () {
    axios('/store/storeDetail', {
      locate: "22.9816,113.72566",
      store_id: app.globalData.storeId
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {

        let full = parseFloat(res.data.result.full_free)

        let send = parseFloat(res.data.result.send_cost)

        let fullFree = ''

        if (parseFloat(this.data.sumMoney) > full) {
          fullFree = '满包邮'
        } else {
          this.setData({
            realityMoney: parseFloat(parseFloat(this.data.realityMoney) + send).toFixed(2)
          })
          fullFree = '+¥' + parseFloat(send).toFixed(2)
        }

        this.setData({
          sendCost: send,
          fullFree: fullFree
        })
      }
    })
  },

  // 买家输入留言事件
  messageInput: function (e) {
    this.setData({
      message: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})